package com.theeviljames.lexer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.theeviljames.syntax.SyntaxAnalyser;

public class Lexer {

	private final Pattern UPPER = Pattern.compile("[A-Z]+");
	private final Pattern NUMBER = Pattern.compile("[0-9]+");
	//private BufferedReader br;
	private int current;
	private String expression;
	private int counter;
	
	public Lexer(String expression) {
/*		File file = new File(filename);
		if(!file.exists()){
			System.out.println("File not found");
		}
		else{
			try{
				br = new BufferedReader(new FileReader(file));
				current = br.read();
				//System.out.println(current);
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}*/
		this.expression = expression; 
		current = expression.charAt(0);
		counter = 1;
	}
	
	private int nextChar(){
		if(counter==expression.length())return -1;
		else return expression.charAt(counter++);
	}
	public Token nextToken(){
		try{
			int c = current;
			current = nextChar();
			if(c==-1){
				//br.close();
				return new Token("EOF",Token.EOF);
			}
			String s = String.valueOf((char)c);
			Matcher m = UPPER.matcher(s);
			if(m.matches()){
				//single identifier
				return new Token(s,Token.IDENTIFIER);
			}
			m = NUMBER.matcher(s);
			if(m.matches()){
				//constant
				while((m = NUMBER.matcher(String.valueOf((char)current))).matches()){
					s += String.valueOf((char)current);
					current = nextChar();
				}
				return new Token(s,Token.CONSTANT);
			}
			if(s.equals("+")){
				return new Token(s,Token.ADD);
			}
			if(s.equals("-")){
				return new Token(s,Token.SUB);
			}
			if(s.equals("*")){
				return new Token(s,Token.MULT);
			}
			if(s.equals("/")){
				return new Token(s,Token.DIV);
			}
			if(s.equals("^")){
				return new Token(s,Token.POW);
			}
			if(s.equals("(")){
				return new Token(s,Token.LPAREN);
			}
			if(s.equals(")")){
				return new Token(s,Token.RPAREN);
			}
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		Pattern floating = Pattern.compile("[0-9]+\\.[0-9]+");
		Matcher m = floating.matcher("214141441.27845");
		System.out.println(m.matches());
	}
}
